<?php

namespace App\Services\Cache;

use App\Http\Controllers\Api\V1\Queries\HotProductQuery;
use App\Http\Controllers\Api\V1\Queries\GroupsProductQuery;
use App\Models\Price;
use App\Models\Age;
use App\Models\Mileage;
use App\Models\Time;
use App\Models\Displacement;
use App\Models\Product;
use App\Models\SeckillProduct;
use Illuminate\Support\Facades\Cache;
use App\Http\Controllers\Api\V1\Queries\ProductQuery;

class ProductCache
{
	public function products()
	{
		$key = url()->full();


		$user = auth('api')->user();
		

		$products = Cache::remember($key, 1, function () use($user) {

			$query = app(ProductQuery::class)->sale()->where("is_hidden",false)->where("is_open",true)->where("is_examine",true);

			if($user &&  $user->latitude){

				$lat = $user->latitude;
				$lng = $user->longitude;
				$distance = cs()->name;



				$query->selectRaw("*,ACOS(SIN(( $lat * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) +COS(( $lat* 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *COS(( $lng* 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6380  AS distance")
     			 ->having('distance', '<=', $distance);

				
			}

			$brand = request('brand_id');
			if($brand){
				$query->where('brand_id',$brand);
			}

			$priceId = request('price_id');
			$price = Price::find($priceId);
			if($price){
				$query->where('price',">=",$price->min)->where('price',"<",$price->max);
			}

			$type = request('type_id');
			if($type){
				$query->where('type_id',$type);
			}

			$standard = request('standard_id');
			if($standard){
				$query->where('standard_id',$standard);
			}

			$ageId = request('age_id');
			$age = Age::find($ageId);
			if($age){
				$query->where('age',">=",$age->min)->where('age',"<",$age->max);
			}

			$mileageId = request('mileage_id');
			$mileage = Mileage::find($ageId);
			if($mileage){
				$query->where('mileage',">=",$mileage->min)->where('mileage',"<",$mileage->max);
			}

			$timeId = request('time_id');
			$time = Time::find($timeId);
			if($time){
				$query->where('times',">=",$time->min)->where('times',"<",$time->max);
			}

			$displacementId = request('displacement_id');
			$displacement = Displacement::find($displacementId);
			if($displacement){
				$query->where('displacement',">=",$displacement->min)->where('displacement',"<",$displacement->max);
			}

			$provinceCode =  request('province_code');
			if($provinceCode){
				$query->where('province_code',$provinceCode);
			}

			$keyword =  request('keyword');
			if($keyword){
				$query->where('title', 'like', "%$keyword%");
			}


			
			$products = $query->orderByDesc('id')->paginate();
			
			
			
			
			return $products;
		});
		return $products;
	}

	public function myProducts()
	{
		$key = url()->full();



		$products = Cache::remember($key, 1, function () {

			$query = request()->user()->products();
			
			$products = $query->orderByDesc('id')->paginate();
			
			
			
			
			return $products;
		});
		return $products;
	}
	
	public function product($id)
	{
		$key = url()->full();
		$product = Cache::remember($key, 1, function () use ($id) {
			return app(ProductQuery::class)->findOrFail($id);
		});
		return $product;
	}
	
	public function drinks()
	{
		$key = url()->full();
		$hots = Cache::remember($key, 1, function () {
			return Product::query()
				->drink()
				->orderByDesc('id')
				->paginate();
		});
		return $hots;
	}

	public function search()
	{
		$keyword = request('keyword');
		$products =  app(ProductQuery::class)->sale()->where('title', 'like', "%$keyword%")->paginate();
		return $products;
	}

	public function displaces($product_id)
	{
		$key = url()->full();

		$money = Product::find($product_id)->price_market;
		$groups = Cache::remember($key, 1, function () use($money){
			return Product::query()->with("skus")
				->join('group_products','products.id','group_products.product_id')
				->where('products.price_market',$money)
				->where('group_products.is_enable',true)
				->orderByDesc('group_products.order')
				->select('products.*','group_products.order','group_products.is_enable')
				->paginate();
		});

		return $groups;
	}
}
